<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2015/12/31
 * Time: 10:53
 */

namespace app\lib;

use Yii;

class BaseModel
{
    public static function query()
    {
        return Yii::$app->db;
    }
    public static function pageSql($sql,$start,$pagesize)
    {
        $end=$start+$pagesize;
        $start=$start+1;


       return "SELECT *  FROM (SELECT tt.*, ROWNUM AS rowno
          FROM (
	          $sql
				   ) tt
         WHERE ROWNUM <= $end) table_alias
            WHERE table_alias.rowno >= $start";

    }
//ps sys_guid 返回的是16位二进制的数据，必须转换为16进制
    function db_uuid(){
        $cmd=self::query()->createCommand("select RAWTOHEX(sys_guid() )  from DUAL");
        return $cmd->queryScalar();
    }
    function uuid($prefix = '')
    {
//        $chars = md5(uniqid(mt_rand(), true));
        $chars =md5(uniqid(md5(microtime(true)),true));
        $uuid = substr($chars, 0, 8) . '-';
        $uuid .= substr($chars, 8, 4) . '-';
        $uuid .= substr($chars, 12, 4) . '-';
        $uuid .= substr($chars, 16, 4) . '-';
        $uuid .= substr($chars, 20, 12);
        return $prefix . $uuid;
    }
    function getDateTimeStr(){
        return date("Y-m-d H:i:s",time());
    }
/*
 * $transaction =  static::query()->beginTransaction();
try {
    //exec
$transaction->commit();
} catch(Exception $e) {
    $transaction->rollBack();
}
*/
}